#include "PKProgressBar.h"

CPKProgressBar::CPKProgressBar(QWidget *parent)
	: QFrame(parent)
{
	ui.setupUi(this);
	InitUI();
}

CPKProgressBar::~CPKProgressBar()
{
}

void CPKProgressBar::InitUI()
{
	if (nullptr == seperator_label_)
	{
		seperator_label_ = new QLabel(this);
		seperator_label_->setObjectName("label_seperator");
	}

	if (nullptr == styleFrame_)
	{
		styleFrame_ = new QFrame(this);
		styleFrame_->raise();
		styleFrame_->setStyleSheet("background-color:red;");
		styleFrame_->setGeometry(this->geometry());
		styleFrame_->setFixedSize(this->width(), this->height());
		QHBoxLayout* styleLayout = new QHBoxLayout(styleFrame_);
		styleLayout->setSpacing(0);
		styleLayout->setMargin(0);
		styleLayout->setContentsMargins(0, 0, 0, 0);
		styleFrame_->setLayout(styleLayout);

		QLabel* leftDiamand = new QLabel(styleFrame_);
		leftDiamand->setObjectName("label_pink_diamand");
		leftDiamand->setFixedSize(9, 9);
		styleLayout->addWidget(leftDiamand);
		leftDiamand->raise();

		leftScoreLabel_ = new QLabel(styleFrame_);
		leftScoreLabel_->setObjectName("label_left_score");
		leftScoreLabel_->setMinimumSize(80, this->height());
		styleLayout->addWidget(leftScoreLabel_);
		leftScoreLabel_->raise();

		QSpacerItem *horicalSpacer;
		horicalSpacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred);
		styleLayout->addItem(horicalSpacer);

		rightScoreLabel_ = new QLabel(styleFrame_);
		rightScoreLabel_->setObjectName("label_right_score");
		rightScoreLabel_->setMinimumSize(80, this->height());
		styleLayout->addWidget(rightScoreLabel_);
		rightScoreLabel_->raise();

		QLabel* rightDiamand = new QLabel(styleFrame_);
		rightDiamand->setObjectName("label_blue_diamand");
		rightDiamand->setFixedSize(9, 9);
		styleLayout->addWidget(rightDiamand);
		rightDiamand->raise();
	}
}

void CPKProgressBar::UpdateProgress(float progress, bool bUseValue)
{
	if (bUseValue)
	{
		progressValue_ = progress;
	}
	float realProgress = 0.5;
	if (progressValue_ >= 0.0 && progressValue_ <= 1.0)
	{
		realProgress = (1.0 - 0.85)*0.5 + progressValue_ * 0.85;
	}

	if (nullptr != seperator_label_)
	{
		seperator_label_->setGeometry(realProgress * width() - 14 / 2, -5, 14, 25);
	}
	ui.label_left_process->setFixedWidth(realProgress * (width()-14));

	if (nullptr != styleFrame_)
	{
		styleFrame_->setGeometry(this->geometry());
		styleFrame_->setFixedSize(this->width(), this->height());
	}
}

void CPKProgressBar::UpdateScoreLabels(long leftScore, long rightScore)
{
	if (nullptr != leftScoreLabel_)
	{
		leftScoreLabel_->setText(QString("%L1").arg(leftScore));
	}
	if (nullptr != rightScoreLabel_)
	{
		rightScoreLabel_->setText(QString("%L1").arg(rightScore));
	}
}